//状态
const STATUS = {
    TRUE: {
        color: "#42b983" //green
    },
    FALSE: {
        color: "red"
    },
    STOP: {
        color: "#FFFF00" //yellow

    },
    DEFAULT: {
        color: "#D9D9D9" //gray
    }
}
/**
 * 画文字函数
 * @param start_x    文字左上顶点的 x
 * @param start_y    文字左上顶点的 y
 * @param content    文字内容
 * @param fontSize   文字font
 * @param lineSpace  文字行间距
 * @param lineWordCount  一行最多文字
 * @param ctx  画布
 */

export function drawText(start_x, start_y, content, font, fontSize, lineSpace, lineWordCount, ctx) {
    let str = content; //文本内容
    let myLineWordCount = lineWordCount; //一行最多字数
    let startOffsetX = start_x;
    let startOffsetY = start_y;
    let myFontSize = fontSize; //字体size
    let myLineSpace = lineSpace; //文本行间距

    if (str == null)
        return;

    //文本从上开始
    ctx.textBaseline = "top";
    //字体
    ctx.font = font;
    //文字颜色
    ctx.fillStyle = "white";
    //文字居中
    ctx.textAlign = "center";

    //如果不用换行
    if (str.length <= myLineWordCount) {
        ctx.fillText(str, startOffsetX, startOffsetY);
    }
    //换行
    else {
        let xOffset = startOffsetX; //文字起始位置
        let yOffset = startOffsetY; //文字起始位置
        for (let i = 0; i < str.length; i += myLineWordCount) {
            let str_start = i;
            let str_end = i + myLineWordCount;
            if (str_end > str.length) {
                str_end = str.length;
            }
            //截取子串
            ctx.fillText(str.slice(str_start, str_end), xOffset, yOffset);
            //换行，y 加上行间距和字体大小
            yOffset += myLineSpace + myFontSize;
        }
    }
    ctx.stroke();
}

/**
 * 画线函数（适用于应急管理）:
 * 当线两端的节点不在同一行或同一列时（画折线），折线的中点在起点和终点的中间
 * @param start_x 起点 x
 * @param start_y 起点 y
 * @param end_x   终点 x
 * @param end_y   终点 y
 * @param startConnector  出口位置
 * @param endConnector  入口位置
 * @param ctx  画布
 *
 */
export function drawLine(start_x, start_y, end_x, end_y, startConnector, endConnector, ctx) {
    let myStart_x = start_x;
    let myStart_y = start_y;
    let myEnd_x = end_x;
    let myEnd_y = end_y;
    let myStartConnector = startConnector;
    let myEndConnector = endConnector;

    let preMiddlePoint = {
        x: 0,
        y: 0
    };
    let middlePoint = {
        x: 0,
        y: 0
    };
    if ( (myStart_y === myEnd_y && myStart_x !== myEnd_x) || (myStart_x === myEnd_x && myStart_y !== myEnd_y)) {
        ctx.beginPath();
        ctx.strokeStyle = "#F6AB6C";
        ctx.moveTo(myStart_x, myStart_y);
        ctx.lineTo(myEnd_x, myEnd_y);
        ctx.stroke();
    } else {
        if (myStartConnector === 2 || myStartConnector === 4) {
            preMiddlePoint.x = myStart_x;
            // preMiddlePoint.y = myEnd_y + (myEndConnector - 3) * m / 2;
            preMiddlePoint.y = myEnd_y + (myEndConnector - 3)  / 2
            middlePoint.x = myEnd_x;
            middlePoint.y = preMiddlePoint.y;
        } else {
            preMiddlePoint.x = myStart_x + (myEnd_x - myStart_x ) / 2;
            preMiddlePoint.y = myStart_y;
            middlePoint.x = preMiddlePoint.x;
            middlePoint.y = myEnd_y;
        }
        ctx.beginPath();
        ctx.strokeStyle = "#F6AB6C";

        ctx.moveTo(myStart_x, myStart_y);
        ctx.lineTo(preMiddlePoint.x, preMiddlePoint.y);
        ctx.lineTo(middlePoint.x, middlePoint.y);
        ctx.lineTo(myEnd_x, myEnd_y);

        ctx.stroke();
    }
}
/**
 * 画线函数（适用于关键回路）:
 * 当线两端的节点不在同一行或同一列时（画折线），折线的中间节点在起点和起点+1节点的中间，或者终点和终点-1节点的中间
 * @param start_x 起点 x
 * @param start_y 起点 y
 * @param end_x   终点 x
 * @param end_y   终点 y
 * @param startConnector  出口位置
 * @param endConnector  入口位置
 * @param ctx  画布
 *
 */
export function drawLine1(start_x, start_y, middle_x, middle_y, end_x, end_y, startConnector, endConnector, ctx) {
    let myStart_x = start_x;
    let myStart_y = start_y;
    let myMiddle_x = middle_x;
    let myMiddle_y = middle_y;
    let myEnd_x = end_x;
    let myEnd_y = end_y;
    let myStartConnector = startConnector;
    let myEndConnector = endConnector;

    let preMiddlePoint = {
        x: myMiddle_x,
        y: myMiddle_y
    };
    let middlePoint = {
        x: 0,
        y: 0
    };
    if ( (myStart_y === myEnd_y && myStart_x !== myEnd_x) || (myStart_x === myEnd_x && myStart_y !== myEnd_y)) {
        ctx.beginPath();
        ctx.strokeStyle = "#ffffff";
        ctx.moveTo(myStart_x, myStart_y);
        ctx.lineTo(myEnd_x, myEnd_y);
        ctx.stroke();
    } else {
        if (myStartConnector === 2 || myStartConnector === 4) {
            preMiddlePoint.x = myStart_x;
            // preMiddlePoint.y = myEnd_y + (myEndConnector - 3) * m / 2;
            preMiddlePoint.y = myEnd_y + (myEndConnector - 3)  / 2
            middlePoint.x = myEnd_x;
            middlePoint.y = preMiddlePoint.y;
        } else {
            //只考虑左进右出的情况
            middlePoint.x = preMiddlePoint.x;
            middlePoint.y = myEnd_y;
        }
        ctx.beginPath();
        ctx.strokeStyle = "#ffffff";

        ctx.moveTo(myStart_x, myStart_y);
        ctx.lineTo(preMiddlePoint.x, preMiddlePoint.y);
        ctx.lineTo(middlePoint.x, middlePoint.y);
        ctx.lineTo(myEnd_x, myEnd_y);

        ctx.stroke();
    }
}
/**
 *
 * @param start_x  左上定点x
 * @param start_y  左上顶点y
 * @param type     节点类型（决定形状）
 * @param status   节点状态
 * @param lineWidth  线宽
 * @param ctx  画布
 * @param color  文字颜色
 * @param backgroundColor  背景颜色
 * @param high  高度
 * @param width  宽度
 */
export function drawNode(start_x, start_y, type, status, lineWidth, ctx, color, backgroundColor, high, width) {
    let myStart_x = start_x;
    let myStart_y = start_y;
    let myType = type;
    let myStatus = status;
    let myLineWidth = lineWidth;
    let myColor = color;
    let myBgColor = backgroundColor;
    let h = high;
    let w = width;

    // console.log("myStatus:",myStatus);
    // console.log("STATUS:",STATUS.TRUE);
    if (myStatus.color == STATUS.DEFAULT.color) {
        // console.log("myStatus:== STATUS.DEFAULT",myStatus,STATUS.DEFAULT);
         myBgColor = "#D9D9D9";
    }else if (myStatus.color == STATUS.TRUE.color) {
        // console.log("myStatus:== STATUS.TRUE",myStatus,STATUS.TRUE);
         myBgColor = "#42b983";
    }

    switch (myType) {
        //类型0：矩形
        case 0: {
            ctx.beginPath();
            ctx.lineWidth = myLineWidth;
            ctx.strokeStyle = myColor;
            ctx.rect(myStart_x, myStart_y, w, h);
            ctx.fillStyle = myBgColor;
            ctx.fill();
            ctx.stroke();
        }
            break;
        //类型1：菱形
        case 1: {
            let point_1_x = myStart_x;
            let point_1_y = myStart_y + h * 0.5;
            let point_2_x = myStart_x + w * 0.5;
            let point_2_y = myStart_y;
            let point_3_x = myStart_x + w;
            let point_3_y = myStart_y + h * 0.5;
            let point_4_x = myStart_x + w * 0.5;
            let point_4_y = myStart_y + h;

            ctx.beginPath();
            ctx.lineWidth = myLineWidth;
            ctx.strokeStyle = myColor;
            ctx.moveTo(point_1_x, point_1_y);
            ctx.lineTo(point_2_x, point_2_y);
            ctx.lineTo(point_3_x, point_3_y);
            ctx.lineTo(point_4_x, point_4_y);
            ctx.lineTo(point_1_x, point_1_y);

            ctx.fillStyle = myBgColor;
            ctx.fill();
            ctx.stroke();
        }
            break;
        //类型2：椭圆
        case 2: {
            //找到内部矩形的四个点
            let delta_x = Math.sqrt(3) / 6 * h;
            let r = delta_x * 2;
            let point_1_x = myStart_x + delta_x;
            let point_1_y = myStart_y + h;
            let point_3_x = myStart_x + w - delta_x;
            let point_3_y = myStart_y;

            //打到两段圆弧的圆心
            let o_1_x = myStart_x + r;
            let o_1_y = myStart_y + h * 0.5;
            let o_2_x = myStart_x + w - r;
            let o_2_y = myStart_y + h * 0.5;

            ctx.beginPath();
            ctx.arc(o_1_x, o_1_y, r, 2 / 3 * Math.PI, 4 / 3 * Math.PI);
            ctx.lineTo(point_3_x, point_3_y);
            ctx.arc(o_2_x, o_2_y, r, 5 / 3 * Math.PI, 1 / 3 * Math.PI);
            ctx.lineTo(point_1_x, point_1_y);

            ctx.fillStyle = myBgColor;
            ctx.fill();
            ctx.stroke();
        }
            break;
        default:
            console.log("不支持的类型");
            break;
    }
}